﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace dzs.Web.distribution
{
    public partial class Add : Web.Admin.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["sy"] != null)
                {
                    try
                    {
                        int gid = (Convert.ToInt32(Request.Params["id"]));
                        txtdnum.Text = Request.Params["sy"];
                    }
                    catch (Exception ex)
                    {

                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('没有可分配项！');closedialog();", true);
                    }

                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('没有可分配项！');closedialog();", true);
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {            
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    dzs.Model.distribution model = new dzs.Model.distribution();
                    int gid = (Convert.ToInt32(Request.Params["id"]));                    
                    decimal dnum = decimal.Parse(this.txtdnum.Text);                    
                    string dbatch = this.txtdbatch.Text;
                    string handling = this.txthandling.Text;
                    decimal charges=0;
                    if(this.txtcharges.Text!="")
                        charges = decimal.Parse(this.txtcharges.Text);
                    string linkman = this.txtlinkman.Text;
                    string linkway = this.txtlinkway.Text;
                    string handmode = this.txthandmode.Text;                    
                    model.gid = gid;
                    model.dnum = dnum;                                  
                    model.dbatch = dbatch;
                    model.handling = handling;
                    model.charges = charges;
                    model.linkman = linkman;
                    model.linkway = linkway;
                    model.handmode = handmode;
                    
                    dzs.BLL.distribution bll = new dzs.BLL.distribution();
                    int uid=bll.getuid(gid);
                    if (uid != 0)
                    { 
                        model.uid = uid;
                        Maticsoft.BLL.monthSupplayPlan bm = new Maticsoft.BLL.monthSupplayPlan();
                        DataSet ds= bm.GetListnums(" and  monthSupplyPlanId="+gid);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            decimal nums = decimal.Parse(ds.Tables[0].Rows[0]["synum"].ToString());
                            if (dnum <= nums)
                            {
                                bll.Add(model);
                                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('分配成功！');closedialog();", true);

                            }
                            else
                            {
                                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('数量已分配完！');closedialog();", true);

                            }
                        }
                        else
                        {
                            bll.Add(model);
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('分配成功！');closedialog();", true);
                        }
                       
                       
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('没有得到仓管员！');closedialog();", true);
                    
                    }
                
               
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('没有可分配项！');closedialog();", true);
            }
        }
        public void btnCancle_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "closedialog();", true);
        }
    }
}
